/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var arr=[];
var intervalSize=300;
function createWall(){
	var oseed=5;
    arr=[];
    var ajax=new Ajax();
    ajax.requestURL = "picwall.action";
    ajax.method = "GET";
    ajax.setVar("action", "loadpic");
    ajax.setVar("flag", "03");
    ajax.onLoading=function(){
        
    }
    ajax.onCompletion=function(){
        var jsonList= JSON.parse(ajax.xmlhttp.responseText);
        var subLenSum=0;
        for(var i=0,len=jsonList.length;i<len;i++){
        	var yearMonth=jsonList[i].key;
        	var subjsonList=jsonList[i].value;
        	var subLen=subjsonList.length;
        	createLabel(subLenSum*1000,yearMonth);
        	for(var j=0;j<subLen;j++){
                var offset=subjsonList[j].picName.lastIndexOf(".");
                createPic(subjsonList[j].picName,"pServer?id="+subjsonList[j].id,
                		subjsonList[j].whRate,subjsonList[j].picDesc,Math.ceil(subLen/oseed),1000* subLenSum);
        	}
        	subLenSum+=Math.ceil(subLen/oseed);
        }
    }
    ajax.runAJAX();
}
window.onload=function(){
    createWall();
}
function createLabel(top,yearmonth){
	var div=document.createElement("div");
	div.style.position="absolute";
	if(top==0){
		div.style.top=top+40+"px";
	}else{
		div.style.top=top+intervalSize+"px";
	}
	div.style.left="10px";
	div.innerHTML=yearmonth.substring(0,4)+"年"+yearmonth.substring(5,6)+"月";
	document.body.appendChild(div);
}


function createPic(picname,imgsrc,rate,desc,seed,prevTop){
    var coodinate=genCoodinate(seed,rate,prevTop);
    var div=document.createElement("div");
    div.style.position="absolute";
    div.style.left=coodinate.l+"px";
    div.style.width=coodinate.w+"px";
    div.style.top=coodinate.t+"px";
    div.style.height=coodinate.h+"px";
    div.style.border="8px solid black";
    div.style.padding="6px";
    var h=desc?coodinate.h-14:coodinate.h-4;
    div.innerHTML+="<div style='text-align:center;'><img style='border:2px solid black' onmouseover=\"this.style.cursor='pointer'\" src='"+imgsrc+"'"
         +"onclick=\"window.open('"+imgsrc+"')\" width='"+(coodinate.w-4)
        +"' height='"+h+"' title='"+picname+"'   alt='"+picname+"'/>"+desc+"</div>";
    document.body.appendChild(div);
}
function genCoodinate(seed,rate,prevTop){
    var l=Math.round(Math.random()* 1000);
    var t;
    if(prevTop==0){
    	t=prevTop+Math.round(Math.random()* seed * 1000);
    }
    else{
    	t=prevTop+intervalSize+Math.round(Math.random()* seed * 1000);
    }
    var wh=genImgWh(rate).split(",");
    var r=l+wh[0]*1;
    var b=t+wh[1]*1;
    while(!checkRange(l,r,t,b)){
        l= Math.round(Math.random()* 1000);
        if(prevTop==0){
        	t=prevTop+Math.round(Math.random()* seed * 1000);
        }
        else{
        	t=prevTop+intervalSize+Math.round(Math.random()* seed * 1000);
        }
        r=l+wh[0]*1;
        b=t+wh[1]*1;
    }
    var item={
        "l":l,
        "r":r,
        "t":t,
        "b":b
    };
    arr.push(item);
    var res={
        "l":l,
        "w":wh[0],
        "t":t,
        "h":wh[1]
    };
    return res;
}
function genImgWh(rate){
    var w;
    var h;
    if(rate==null)
        rate=1.33;
    if(rate>1){
        w=400;
        h=w/rate;
    }else{
        h=400;
         w= rate*h;
    }
    var r= Math.random()*0.2+0.8;
    return Math.round(w*r) +","+Math.round(h*r);
}

function checkRange(left,right,top,bottom){
    for(var i=0,len=arr.length;i<len;i++){
        var obj=arr[i];
        var l= obj.l*1-40;
        var r=obj.r *1+40;
        var t=obj.t*1-40;
        var b= obj.b*1+40;
        // 重合
        if(((right>=l && left<=l) || (left<=r && right>=r) || (left>=l && right<=r) ||(left<=l && right>=r) )
            && ((bottom>=t && top<=t) || (top<=b && bottom>=b) || (top>=t && bottom<=b) ||(top<=t && bottom>=b) ) ){
            // alert(left+","+right+","+top+","+bottom+"||||"+l+","+r+","+t+","+b);
            return false;
        }
    }
    return true;
}